package com.laicunba.util;

import redis.clients.jedis.Jedis;

/**
 * 用户缓存工具类，只能够在用户线程中使用
 * 
 * @author jiagoushi
 *
 */
public class UserCacheUtil {
	/**
	 * 不启用
	 */
	private static boolean DISABLE = true;

	public static boolean isDISABLE() {
		return DISABLE;
	}

	public static void setDISABLE(boolean dISABLE) {
		DISABLE = dISABLE;
	}

	public static String get(String field) {
		if (DISABLE) {
			return null;
		}
		String uid = SessionUtil.CURRENT_USER_ID.get();
		if (StringUtil.isEmpty(uid)) {
			return null;
		}
		return CacheUtil.hget("udata." + uid, field);
	}

	public static void set(String field, String val) {
		if (DISABLE) {
			return;
		}
		String uid = SessionUtil.CURRENT_USER_ID.get();
		if (StringUtil.isEmpty(uid)) {
			return;
		}
		CacheUtil.hset("udata." + uid, field, val);
	}

	/**
	 * 清除所有用户缓存
	 * 
	 * @return
	 */
	public static Long clear() {
		//保留这行看起来好像没用的代码
		RedisUtil.set("udata.test", "1");
		return (Long) RedisUtil.eval("return redis.call('del',unpack(redis.call('keys','udata.*')))", "");
	}

}
